﻿using DevExpress.XtraGrid.Demos.util;
using DXApplicationTangche.DTO;
using mendian;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DXApplicationTangche.Utils.UC
{
    public class DataTableService
    {
        /// <summary>
        /// 根据类别取得所有data table的配置
        /// </summary>
        /// <returns></returns>
        public static List<DataTableQueryConfigDao> getDataTableQueryConfigByType(String type)
        {
            return
                FunctionHelper.JsonDeserialization<List<DataTableQueryConfigDao>>(
                    RestCall.httpGetMethod(
                        URL.接口url1 + "/datatable/getDataTableQueryConfigByType?type=" + type
                        )
                    );
        }
        /// <summary>
        /// 释放线程池
        /// </summary>
        /// <param name="sessionId"></param>
        /// <returns></returns>
        public static void removeDataTableThread(String sessionId)
        {
            RestCall.httpGetMethod(
                URL.接口url1 + "/datatable/removeDataTableThread?sessionId=" + sessionId
            );
        }
        /// <summary>
        /// 根据session id取数据
        /// </summary>
        /// <returns></returns>
        public static DataTableQueryConfigDTO getDataBysessionId(String sessionId,String id,Dictionary<String,String> parameter)
        {
            return
                FunctionHelper.JsonDeserialization<DataTableQueryConfigDTO>(
                    RestCall.PostHttp(
                        URL.接口url1 + "/datatable/getDataBysessionId?sessionId=" + sessionId + "&id=" + id,
                        FunctionHelper.JsonSerialization(parameter)
                    )
                );
        }

        /// <summary>
        /// 转dataTable
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static DataTable ToDataTable(List<Dictionary<String, String>> data, List<MetaDataDTO> metaDataDTOs)
        {
            DataTable dt = new DataTable();
            foreach (var item in metaDataDTOs)
            {
                dt.Columns.Add(new DataColumn(item.name, Type.GetType(item.formatType)));
            }
            foreach (var item in data)
            {
                DataRow dr = dt.NewRow();
                foreach (var ii in item)
                {
                    dr[ii.Key] = ii.Value;
                }
                dt.Rows.Add(dr);
            }
            return dt;
        }



    }
    /// <summary>
    /// 通用data table查询结构
    /// </summary>
    public class DataTableQueryConfigDao
    {
        public String id { get; set; }
        public String name { get; set; }
        public String url { get; set; }
        public String type { get; set; }
        public List<MetaDataDTO> metaDataDTOS { get; set; } = new List<MetaDataDTO>();
        public List<ParaDefineDTO> paraDefineDTOS { get; set; } = new List<ParaDefineDTO>();   //  参数字段定义
        public DataTableQueryConfigDao()
        {

        }
    }
    /// <summary>
    /// datatable的map定义
    /// </summary>
    public class DataTableQueryConfigDTO
    {
        public List<Dictionary<String, String>> datas { get; set; } = new List<Dictionary<String, String>>();  //  数据
        public List<MetaDataDTO> metaDataDTOS { get; set; } = new List<MetaDataDTO>(); //  元数据定义
        public DataTableQueryConfigDTO()
        {

        }
    }
    /// <summary>
    /// 元数据定义
    /// </summary>
    public class MetaDataDTO
    {
        public String name { get; set; }
        public String caption { get; set; }
        public Boolean visible { get; set; }
        public String formatType { get; set; }
        public MetaDataDTO()
        {

        }
    }
    /// <summary>
    /// 参数定义
    /// </summary>
    public class ParaDefineDTO
    {
        public String paraId { get; set; }
        public String paraName { get; set; }
        public String paraType { get; set; }
        public ParaDefineDTO()
        {

        }
    }
}
